package net.test.piggy.controller;

import net.test.piggy.listener.MyEventSourceListener;
import net.test.piggy.model.Result;
import net.test.piggy.util.NumberEncryptionUtil;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.sse.EventSource;
import okhttp3.sse.EventSources;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;


@Controller
public class GatewayController {
    @ResponseBody
    @PostMapping("/gateway/color")
    public String test001(HttpServletRequest request){
        String header = request.getHeader("X-Request-Red");
        return "001";
    }

    @ResponseBody
    @RequestMapping("/test/color")
    public String test002(){
        return "002";
    }




    @RequestMapping(value = "/push")
    public void push(HttpServletRequest request, HttpServletResponse response) throws IOException, InterruptedException {
        String authType = request.getHeader("Accept");
        response.setContentType("text/event-stream");
        response.setCharacterEncoding("utf-8");
        for (int i = 0; i < 3; i++) {
            response.getWriter().write("data --> "+i+" --> "+authType+ "\n");
            response.getWriter().flush();
            TimeUnit.SECONDS.sleep(1);
        }
    }

    @RequestMapping(value = "/send")
    public void send(HttpServletResponse response) throws InterruptedException {
        CountDownLatch countDownLatch = new CountDownLatch(1);
        response.setContentType("text/event-stream");
        response.setCharacterEncoding("utf-8");
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .connectTimeout(10, TimeUnit.SECONDS)//链接超时为2秒，单位为秒
                .writeTimeout(10, TimeUnit.SECONDS)//写入超时
                .readTimeout(10, TimeUnit.SECONDS)//读取超时
                .build();
        String json = "{\n" +
                "  \"messages\": [\n" +
                "    {\"role\":\"user\", \"content\": \"写首诗\"}\n" +
                "  ],\n" +
                "  \"stream\": true\n" +
                "}";
        MediaType mediaType = MediaType.parse("application/json");
        RequestBody requestBody = RequestBody.create(json,mediaType);
        String url = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/completions_pro";
        Request request = new Request.Builder()
                .addHeader("Content-Type", "application/json")
                .url(url +"?access_token=" + "24.150a415dafb0fcd058c448c405c861ef.2592000.1708688627.282335-46546159")
                .method("POST", requestBody)
                .build();
        EventSource.Factory factory = EventSources.createFactory(okHttpClient);
        factory.newEventSource(request,new MyEventSourceListener(response,countDownLatch));
        countDownLatch.await();
    }



}
